﻿using AutoClickTool.Common;
using AutoClickTool.Enums;
using AutoClickTool.Models;

namespace AutoClickTool.Hendlers
{
    public class VirtualKeyHendler: IHendler
    {
        private readonly WindowInputHelper _simulator;

        public VirtualKeyHendler(WindowInputHelper simulator)
        {
            this._simulator = simulator;
        }
        /// <summary>
        /// 执行动作
        /// </summary>
        /// <param name="clickModel"></param>
        public void ProcessAction(ClickModel clickModel, IntPtr? targetWindowHandle=null)
        {
            if (!clickModel.AutoUp)
            {
                return;
            }
            var sim = _simulator;
            if ((clickModel.WaitTime ?? 1) == 1 )
            {
                KeyHandler(clickModel, targetWindowHandle);
            }
            else
            {
                for (int i = 0; i < clickModel.LoopNum; i++)
                {
                    if (clickModel.WaitTime.HasValue)
                    {
                        sim = sim.Sleep(clickModel.WaitTime.Value);
                    }
                    KeyHandler(clickModel, targetWindowHandle);
                }
            }
        }
        /// <summary>
        /// 鼠标/键盘
        /// </summary>
        /// <param name="clickModel"></param>
        /// <param name="targetWindowHandle"></param>
        /// <param name="sim"></param>
        private  void KeyHandler(ClickModel clickModel, IntPtr? targetWindowHandle=null)
        {
           
            if (clickModel.ActionType != ActionTypeEnum.PlayBack)
            {
                if (!clickModel.KeyCode.HasValue)
                {
                    throw FriendlyException.Show("动作类型为键盘/鼠标的时候按键名称【KeyCode】不能为空");
                }
                if (clickModel.ActionType== ActionTypeEnum.Mouse)
                {
                    _simulator.MousePress(clickModel.KeyCode.Value, targetWindowHandle);

                }
                if (clickModel.ActionType== ActionTypeEnum.Key)
                {
                    _simulator.KeyPress(clickModel.KeyCode.Value, targetWindowHandle);
                }
            }
        }
        public void SendText(ClickModel clickModel, IntPtr? targetWindowHandle = null)
        {
            if (clickModel.ActionType==ActionTypeEnum.Text) {
                _simulator.SendText(clickModel.TextValue!, targetWindowHandle);
            }    
        }
        /// <summary>
        /// 按下
        /// </summary>
        /// <param name="clickModel"></param>
        /// <param name="targetWindowHandle"></param>
        public void ProcessActionDown(ClickModel clickModel, IntPtr? targetWindowHandle = null)
        {
            if (clickModel.ActionType != ActionTypeEnum.PlayBack)
            {
                if (!clickModel.KeyCode.HasValue)
                {
                    throw FriendlyException.Show("动作类型为键盘/鼠标的时候按键名称【KeyCode】不能为空");
                }
                if (clickModel.ActionType == ActionTypeEnum.Mouse)
                {
                    if (clickModel.AutoUp)
                        _simulator.MouseDown(clickModel.KeyCode.Value, targetWindowHandle);

                }
                if (clickModel.ActionType == ActionTypeEnum.Key)
                {
                    _simulator.KeyDown(clickModel.KeyCode.Value, targetWindowHandle);

                }
            }
        }
        /// <summary>
        /// 抬起
        /// </summary>
        /// <param name="clickModel"></param>
        /// <param name="targetWindowHandle"></param>
        public void ProcessActionUp(ClickModel clickModel, IntPtr? targetWindowHandle = null)
        {
            if (clickModel.ActionType != ActionTypeEnum.PlayBack)
            {
                if (!clickModel.KeyCode.HasValue)
                {
                    throw FriendlyException.Show("动作类型为键盘/鼠标的时候按键名称【KeyCode】不能为空");
                }
                if (clickModel.ActionType == ActionTypeEnum.Mouse)
                {
                    if (clickModel.AutoUp)
                        _simulator.MouseUp(clickModel.KeyCode.Value, targetWindowHandle);

                }
                if (clickModel.ActionType == ActionTypeEnum.Key)
                {
                    _simulator.KeyUp(clickModel.KeyCode.Value, targetWindowHandle);

                }
            }
        }
    }
}
